package com.web;

import springfox.documentation.swagger2.annotations.EnableSwagger2;
import io.swagger.models.Contact;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@EnableSwagger2
@Configuration
public class Swagger2Config {

    //api接口包扫描路径
    private static final String SWAGGER_SCAN_BASE_PACKAGE = "com.web.web";
    //
    private static final String VERSION = "2.0";

    @Bean
    public Docket createRestApi(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //组名,分组
                .groupName("默认分组")
                //是否接受被访问 默认true
//                .enable(false)
                .select()
                //扫描包路径
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                //可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact();
        contact.setName("hgjxyz");
        contact.setEmail("2068006342@qq.com");
        contact.setUrl("https://www.baidu.com");
        return new ApiInfoBuilder()
                // 页面标题
                .title("桔子云笔记")
                // 创建人
                .contact(contact.toString())
                // 描述
                .description("")
                // 版本
                .version(VERSION)
                // 文档的信息
                .termsOfServiceUrl("https://www.baidu.com")
                .build();
    }
}
